using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace CarbonExchange.Bll.Exceptions
{
    [global::System.Serializable]
    public class ValidationException : Exception
    {
        //
        // For guidelines regarding the creation of new exception types, see
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconerrorraisinghandlingguidelines.asp
        // and
        //    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dncscol/html/csharp07192001.asp
        //

        public ValidationException() { }
        public ValidationException(string message)
            : base(message)
        {
            Add(message);
        }
        public ValidationException(string message, Exception inner)
            : base(message, inner)
        {
            Add(message);
        }
        protected ValidationException(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

        private List<ValidationExceptionItemBase> items = new List<ValidationExceptionItemBase>();
        public ValidationExceptionItemBase[] Items
        {
            get { return items.ToArray(); }
        }

        public bool HasDuplicateEmailAddressValidationExceptionItem()
        {
            return this.DuplicateEmailAddressValidationExceptionItems().Count() > 0;
        }

        public IQueryable<DuplicateEmailAddressValidationExceptionItem> DuplicateEmailAddressValidationExceptionItems()
        {
            return items.AsQueryable().OfType<DuplicateEmailAddressValidationExceptionItem>();
        }

        public bool HasNotApprovedEmailAddressValidationExceptionItem()
        {
            return this.NotApprovedEmailAddressValidationExceptionItems().Count() > 0;
        }

        public IQueryable<NotApprovedEmailAddressValidationExceptionItem> NotApprovedEmailAddressValidationExceptionItems()
        {
            return items.AsQueryable().OfType<NotApprovedEmailAddressValidationExceptionItem>();
        }

        public bool HasValidationExceptionItem()
        {
            return ValidationExceptionItems().Count() > 0;
        }

        public IQueryable<ValidationExceptionItem> ValidationExceptionItems()
        {
            return items.AsQueryable().OfType<ValidationExceptionItem>();
        }

        public bool HasInvalidEmailAddressValidationExceptionItem()
        {
            return InvalidEmailAddressValidationExceptionItems().Count() > 0;
        }

        public IQueryable<InvalidEmailAddressValidationExceptionItem> InvalidEmailAddressValidationExceptionItems()
        {
            return items.AsQueryable().OfType<InvalidEmailAddressValidationExceptionItem>();
        }

        public bool HasUnhandledExceptionItem()
        {
            return UnHandledValidationExceptionItems().Count() > 0;
        }

        public IQueryable<ValidationExceptionItemBase> UnHandledValidationExceptionItems()
        {
            return items.AsQueryable().Where(x => !x.Handeled);
        }

        public void Add(string message)
        {
            Add(new ValidationExceptionItem(message));
        }

        public void Add(ValidationExceptionItemBase item)
        {
            items.Add(item);
        }

        public override string Message
        {
            get
            {
                var tmp = items.ToFormatedStringWithSpacer(":\n", x => x.Message);
                return tmp.ToString();
            }
        }
    }

    public abstract class ValidationExceptionItemBase : Exception
    {
        protected ValidationExceptionItemBase() { }
        protected ValidationExceptionItemBase(string message) : base(message) { }
        protected ValidationExceptionItemBase(string message, Exception inner) : base(message, inner) { }
        protected ValidationExceptionItemBase(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

        public bool Handeled { get; set; }
    }

    public class ValidationExceptionItem : ValidationExceptionItemBase
    {
        public ValidationExceptionItem() { }
        public ValidationExceptionItem(string message) : base(message) { }
        public ValidationExceptionItem(string message, Exception inner) : base(message, inner) { }
        protected ValidationExceptionItem(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

    }

    public class DuplicateEmailAddressValidationExceptionItem : ValidationExceptionItemBase
    {
        public DuplicateEmailAddressValidationExceptionItem() { }
        public DuplicateEmailAddressValidationExceptionItem(string message) : base(message) { }
        public DuplicateEmailAddressValidationExceptionItem(string message, Exception inner) : base(message, inner) { }
        protected DuplicateEmailAddressValidationExceptionItem(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

    }

    public class NotApprovedEmailAddressValidationExceptionItem : ValidationExceptionItemBase
    {
        public NotApprovedEmailAddressValidationExceptionItem() { }
        public NotApprovedEmailAddressValidationExceptionItem(string message) : base(message) { }
        public NotApprovedEmailAddressValidationExceptionItem(string message, Exception inner) : base(message, inner) { }
        protected NotApprovedEmailAddressValidationExceptionItem(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

    }

    public class InvalidEmailAddressValidationExceptionItem : ValidationExceptionItemBase
    {
        public InvalidEmailAddressValidationExceptionItem() { }
        public InvalidEmailAddressValidationExceptionItem(string message) : base(message) { }
        public InvalidEmailAddressValidationExceptionItem(string message, Exception inner) : base(message, inner) { }
        protected InvalidEmailAddressValidationExceptionItem(
          System.Runtime.Serialization.SerializationInfo info,
          System.Runtime.Serialization.StreamingContext context)
            : base(info, context) { }

    }
}
